package ReentrantLock;

import java.util.concurrent.TimeUnit;

/**
 * @功能
 * @创建时间 2019/12/19
 * 重入锁 reentrantlock用于替代synchronized
 * 本例中用m1锁定this，只有m1执行完毕的时候，m2才会执行
 * 这里是复习synchronized最原始的语义
 */
public class ReentrantLock1 {

    synchronized void m1(){
        for (int i=0;i<10;i++){
            try{
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(i);
        }
    }
    synchronized void m2(){
        System.out.println("m2....");
    }

    public static void main(String[] args) {
        ReentrantLock1 r1 = new ReentrantLock1();
        new Thread(r1::m1,"r1").start();
        try{
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(r1::m2,"m2").start();
    }
}
